﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ChildrenLoadedHandler = TreeBuilder.Contracts.ChildrenLoadedHandler;
using AbstraX.ClientInterfaces;
using TreeBuilder.Contracts;
using ServerEntities = SolutionFramework.Entities;
using System.ServiceModel.DomainServices.Client;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Windows.Media.Imaging;
using UIObjects.TreeViewInterfaces;

namespace SolutionFramework.Entities.TreeBuilder.Client
{
    public class TreeNodeImage : ITreeNodeImage
    {
        private ServerEntities.TreeNodeImage treeNodeImage;
        private ITreeBuilderService service;
        private ObservableCollection<ITreeNodeBase> children;
        private IBase queryInput;
        private List<IBase> queryReturn;
        private TreeNodeImageFormat imageFormat;
        private object image;
        public event NodeOperationSuccessful RemovedSuccessfully;
        public event NodeOperationSuccessful UpdatedSuccessfully;

        public TreeNodeImage(ITreeBuilderService service, ServerEntities.TreeNodeImage treeNodeImage)
        {
            this.treeNodeImage = treeNodeImage;
            this.service = service;

            var op = service.GetTreeNodeImageFormat(this);

            op.Invoked += (sender, e) =>
            {
                imageFormat = e.ReturnValue;
            };
        }

        public T GetImage<T>()
        {
            if (image == null)
            {
                image = service.ConvertImage<T>(this.ImageData);
            }

            return (T) image;
        }

        public byte[] ImageData
        {
            get
            {
                return treeNodeImage.ImageData;
            }
            set
            {
                treeNodeImage.ImageData = value;
                service.UpdateImage(this);
            }
        }

        public Entity ServerEntity
        {
            get 
            {
                return treeNodeImage;
            }
        }

        public string ID
        {
            get 
            {
                return treeNodeImage.TreeNodeImageID.ToString();
            }
        }

        public void NoChildren()
        {
            throw new NotImplementedException();
        }

        public ITreeNodeServerEntity ShallowClone()
        {
            throw new NotImplementedException();
        }

        public ITreeNodeServerEntity DeepClone()
        {
            throw new NotImplementedException();
        }

        public TreeNodeImageFormat ImageFormat
        {
            get
            {
                return imageFormat;
            }
            set
            {
                var op = service.GetServerImageFormat(this);

                op.Invoked += (sender, e) =>
                {
                    treeNodeImage.ImageFormat = e.ReturnValue;
                    service.UpdateImage(this);
                };
            }
        }

        public Guid ImageKey
        {
            get
            {
                return (Guid) treeNodeImage.ImageKey;
            }
            set
            {
                treeNodeImage.ImageKey = value;
                service.UpdateImage(this);
            }
        }

        public string FileName
        {
            get
            {
                return treeNodeImage.FileName;
            }
            set
            {
                treeNodeImage.FileName = value;
                service.UpdateImage(this);
            }
        }

        public Guid? ImageServerFormat
        {
            get
            {
                return treeNodeImage.ImageFormat;
            }
            set
            {
                treeNodeImage.ImageFormat = value;
                service.UpdateImage(this);
            }
        }
    }
}
